#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// 选择排序函数
void selection_sort(int arr[], int n) {
    for (int i = 0; i < n - 1; i++) {
        int min_index = i;
        for (int j = i + 1; j < n; j++) {
            if (arr[j] < arr[min_index]) {
                min_index = j;
            }
        }
        if (min_index!= i) {
            // 交换元素
            int temp = arr[i];
            arr[i] = arr[min_index];
            arr[min_index] = temp;
        }
    }
}

int main() {
    int data_sizes[] = {10000, 100000, 1000000};
    for (int k = 0; k < 3; k++) {
        int size = data_sizes[k];
        int *data = (int *)malloc(size * sizeof(int));
        if (data == NULL) {
            printf("内存分配失败！\n");
            return 1;
        }
        // 生成随机数填充数组
        srand((unsigned int)time(NULL));
        for (int i = 0; i < size; i++) {
            data[i] = rand() % 1000001;
        }

        clock_t start_time = clock();
        // 调用选择排序函数
        selection_sort(data, size);
        clock_t end_time = clock();

        double elapsed_time = ((double)(end_time - start_time)) / CLOCKS_PER_SEC;
        printf("%d 数据量选择排序耗时: %.2f 秒\n", size, elapsed_time);

        free(data);
    }
    return 0;
}